<template>
	<view class="goods_item_box" :class="[showRadio ? 'goods_item_box_radio' : '']">
		<radio :checked="item.goods_state" color="#007AFF" v-if="showRadio" @click="radioChange" />
		<!-- left image box -->
		<view class="img_box" :class="[showRadio ? 'goods_item_box_radio' : '']">
			<image :src="item.goods_small_logo || defaultPic" mode="widthFix" />
		</view>
		<!-- right content box -->
		<view class="content_box">
			<view class="goods_name">{{ item.goods_name }}</view>
			<view class="goods_price">￥{{ item.goods_price | tofixed }}</view>
			<uni-number-box class="number_box" :min="1" :value="item.goods_count" v-if="showRadio" @change="numChangeHandler" />
		</view>
	</view>
</template>

<script>
	import { mapMutations } from 'vuex'
	export default {
		name: "goods-items",
		props: {
			item: {
				type: Object,
				default: {}
			},
			showRadio: {
				type: Boolean,
				default: false
			}
		},
		// 过滤器
		filters: {
			tofixed(num) {
				return Number(num).toFixed(2)
			}
		},
		data() {
			return {
				defaultPic: '/static/logo.png',
			};
		},
		methods: {
			...mapMutations('store_cart', ['isCheckAll']),
			
			// 监听单选框的改变事件
			radioChange() {
				this.$emit('radio-Change', {
					goods_id: this.item.goods_id,
					goods_state: !this.item.goods_state
				})
				
				this.isCheckAll()
			},
			
			// 监听商品数量的改变
			numChangeHandler(val) {
				// console.log(val)
				this.$emit('num-Change', {
					goods_id: this.item.goods_id,
					goods_count: val
				})
			}
		}
	}
</script>

<style lang="scss">
	.goods_item_box {
		display: flex;
		flex-direction: row;
		border-bottom: 1px solid #d6d6d6;
		.img_box {
			width: 25%;
			margin: 5px;
			image {
				width: 100%;
				border-radius: 5px;
			}
		}
		.content_box {
			position: relative;
			width: 70%;
			height: 200rpx;
			padding: 15rpx 20rpx 0 20rpx;
			.goods_name {
				font-size: 14px;
			}
			.goods_price {
				position: absolute;
				bottom: 0;
				font-size: 16px;
				color: #e80000
			}
			.number_box {
				position: absolute;
				right: 5px;
				bottom: 5px;
			}
		}
	}
</style>

<style>
	.goods_item_box_radio {
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	
	.img_box_radio {
		width: 30%;
		margin: 0;
	}
</style>
